package com.geeklan.smart.ui.feed.fragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.R;
import com.geeklan.smart.common.Constants;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.databinding.AdapterFeedingPlanItemBinding;
import com.geeklan.smart.widget.ItemOnClickListener;

import java.util.List;

public class FeedingPlanAdapter extends RecyclerView.Adapter<FeedingPlanAdapter.ItemViewHolder> {
    private Context mContext;
    private List<FeedRule> itemList;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public FeedingPlanAdapter(Context context, List<FeedRule> itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FeedingPlanAdapter.ItemViewHolder(AdapterFeedingPlanItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        FeedRule timedPlan = itemList.get(position);
        holder.mBinding.tvTime.setText(timedPlan.getStartTime());
        holder.mBinding.scIsOpen.setChecked(timedPlan.getDateStatus() == 1);

        switch (timedPlan.getFeedStatus()) {
            case 0:
                holder.mBinding.ivFeedPlanStatus.setImageResource(R.drawable.ic_feed_plan_wait);
                holder.mBinding.tvStatus.setText(mContext.getString(R.string.wait_feeding));
                holder.mBinding.tvFeedPlanMsg.setTextColor(mContext.getColor(R.color.color_999999));
                holder.mBinding.tvTime.setTextColor(mContext.getColor(R.color.color_999999));
                holder.mBinding.scIsOpen.setVisibility(View.VISIBLE);
                break;
            case 1:
                holder.mBinding.ivFeedPlanStatus.setImageResource(R.drawable.ic_feed_plan_complete);
                holder.mBinding.tvStatus.setText(mContext.getString(R.string.feed_ok));
                holder.mBinding.tvFeedPlanMsg.setTextColor(mContext.getColor(R.color.color_333333));
                holder.mBinding.tvTime.setTextColor(mContext.getColor(R.color.color_333333));
                holder.mBinding.scIsOpen.setVisibility(View.GONE);
                break;
            case 2:
                holder.mBinding.ivFeedPlanStatus.setImageResource(R.drawable.ic_feed_plan_cancel);
                holder.mBinding.tvStatus.setText(mContext.getString(R.string.cancel_feeding));
                holder.mBinding.tvFeedPlanMsg.setTextColor(mContext.getColor(R.color.color_333333));
                holder.mBinding.tvTime.setTextColor(mContext.getColor(R.color.color_333333));
                holder.mBinding.scIsOpen.setVisibility(View.GONE);
                break;
        }

        if (timedPlan.getFeedType() == 0) {
            String string = mContext.getString(R.string.pet_maotiao) + "*" + timedPlan.getFeedNum();
            holder.mBinding.tvFeedPlanMsg.setText(string);
        } else {
            String string = mContext.getString(R.string.pet_food) + "*" + timedPlan.getFeedNum() + "约" + (timedPlan.getFeedNum() * Constants.FOOD_WEIGHT) + "g";
            holder.mBinding.tvFeedPlanMsg.setText(string);
        }


        holder.mBinding.getRoot().setOnClickListener(v -> {
            itemOnClickListener.OnItemClick(v, position);
        });
        holder.mBinding.scIsOpen.setOnClickListener(v -> {
            itemOnClickListener.isOpen(holder.mBinding.scIsOpen.isChecked(), position);
        });


    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        AdapterFeedingPlanItemBinding mBinding;

        public ItemViewHolder(@NonNull AdapterFeedingPlanItemBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }
    }

    public interface ItemOnClickListener {
        void OnItemClick(View view, int position);

        void isOpen(boolean isOpen, int position);
    }
}